package com.sky.service.impl;

import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 陈烨庆
 * @version 1.0
 */
@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public BusinessDataVO businessData(LocalDateTime startTime, LocalDateTime endTime) {
        Map<String, Object> map = new HashMap<>();
        map.put("begin", startTime);
        map.put("end", endTime);
        Integer todayUserCount = userMapper.getCountByDate(map);

        Integer allStatusCount = orderMapper.selectCountByDate(map);

        map.put("status", Orders.COMPLETED);
        Integer completedCount = orderMapper.selectCountByDate(map);
        List<Orders> list = orderMapper.selectCountByMap(map);

        double orderCompletionRate = 0.0d;
        if (allStatusCount != 0) {
            orderCompletionRate = (double) completedCount / allStatusCount;
        }

        BigDecimal amount = BigDecimal.ZERO;
        if (!CollectionUtils.isEmpty(list)) {
            if (!ObjectUtils.isEmpty(amount)) {
                amount = list.get(0).getAmount();
            }
        }

        Double unitPrice = 0.0d;
        if (completedCount != 0) {
            unitPrice = amount.doubleValue() / completedCount;
        }

        return BusinessDataVO.builder()
                .newUsers(todayUserCount)
                .validOrderCount(completedCount)
                .orderCompletionRate(orderCompletionRate)
                .turnover(amount.doubleValue())
                .unitPrice(unitPrice)
                .build();
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Setmeal setmeal = new Setmeal();
        // 禁用
        setmeal.setStatus(0);
        List<Setmeal> list1 = setmealMapper.list(setmeal);

        // 启用
        setmeal.setStatus(1);
        List<Setmeal> list2 = setmealMapper.list(setmeal);

        return SetmealOverViewVO.builder()
                .discontinued(list1.size())
                .sold(list2.size())
                .build();
    }

    @Override
    public DishOverViewVO overviewDishes() {
        Dish dish = new Dish();
        // 禁售
        dish.setStatus(0);
        List<Dish> list1 = dishMapper.list(dish);

        // 起售
        dish.setStatus(1);
        List<Dish> list2 = dishMapper.list(dish);
        return DishOverViewVO.builder()
                .discontinued(list1.size())
                .sold(list2.size())
                .build();
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        LocalDateTime startTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);

        Map<String, Object> map = new HashMap<>();
        map.put("begin", startTime);
        map.put("end", endTime);

        Integer allCount = orderMapper.selectCountByDate(map);

        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer count2 = orderMapper.selectCountByDate(map);
        map.put("status", Orders.CONFIRMED);
        Integer count4 = orderMapper.selectCountByDate(map);
        map.put("status", Orders.COMPLETED);
        Integer count5 = orderMapper.selectCountByDate(map);
        map.put("status", Orders.CANCELLED);
        Integer count6 = orderMapper.selectCountByDate(map);

        return OrderOverViewVO.builder()
                .allOrders(allCount)
                .cancelledOrders(count6)
                .completedOrders(count5)
                .deliveredOrders(count4)
                .waitingOrders(count2)
                .build();
    }
}
